import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'

declare var $: any
@Component({
  selector: 'sa-group-message',
  templateUrl: './group-message.component.html'
})
export class GroupMessageComponent implements OnInit {
  tableData: any[] = [] //table数据
  totalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  tableParam: any = {
    startTime: '',
    endTime: '',
    handler: ''
  }
  carList: any[] = []
  isShowDelete = false // 默认不显示删除按钮
  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {
      this.curPage = queryParams.nowPage || 1
    })
  }
  ngOnInit() {
    this.load()
    this.initDate()
  }
  //初始化日期
  initDate() {
    const self = this
    System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(() => {
      System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(() => {
        $('#queStartTime')
          .datetimepicker({
            //时间插件
            format: 'yyyy-mm-dd hh:ii:ss',
            language: 'zh-CN',
            autoclose: true
          })
          .on('changeDate', function (e) {
            const date = e.date
            if (!date) {
              return
            }
            const time = common.getTimeMmss(date)
            self.messageEditForm.queStartTime = time
          })
        $('#queEndTime')
          .datetimepicker({
            //时间插件
            format: 'yyyy-mm-dd hh:ii:ss',
            language: 'zh-CN',
            autoclose: true
          })
          .on('changeDate', function (e) {
            const date = e.date
            if (!date) {
              return
            }
            const time = common.getTimeMmss(date)
            self.messageEditForm.queEndTime = time
          })
      })
    })
  }
  //table刷新
  refresh() {
    this.load()
  }
  //表格搜索
  tableSearch() {
    this.curPage = 1
    this.load()
  }
  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  //初始化加载list数据
  load() {
    const tableParam = this.tableParam
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        startTime: tableParam.startTime ? tableParam.startTime + ' 00:00:00' : '',
        endTime: tableParam.endTime ? tableParam.endTime + ' 23:59:59' : '',
        handler: tableParam.handler
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('groupMessage/queryGroupMessageList.html', reqdata).subscribe(data => {
      if (data.isSuccess) {
        this.tableData = data.exhibitDatas
        this.totalCount = data.recordCount
      } else {
        this.jsonApiService.message(data.message)
      }
    })
  }

  // 数据导出
  exportData() {
    if (!this.tableParam.startTime || !this.tableParam.endTime) {
      this.jsonApiService.message('请先选择时间区间再进行导出')
      return
    }
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        startTime: this.tableParam.startTime,
        endTime: this.tableParam.endTime,
        handler: this.tableParam.handler
      },
      isExport: 1
    }
    common.exprotDataFile(data, 'gridPager', config.BaseURL + 'groupMessage/queryGroupMessageList.html')
  }

  // 删除
  deleteRow(row) {
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除?').then(() => {
      let reqdata = 'id=' + row.id
      this.jsonApiService.http('groupMessage/deleteGroupMessageById.html', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        this.load()
      })
    })
  }
  // 接收子组件传递过来的数据
  getSelectTableTime(time) {
    this.tableParam.startTime = time.startTime
    this.tableParam.endTime = time.endTime
  }

  // 删除输入
  delKey() {
    this.tableParam.handler = ''
  }

  activeSelected: string = ''
  startTime_chart: string = ''
  endTime_chart: string = ''
  isChartView = false
  selectedRow: any
  //画轨迹的数据
  drawTrackArr: any
  isMapView = false

  specifyId = {
    picker1: 'datetimepicker3',
    picker2: 'datetimepicker4'
  }

  specifyTime = {
    startTime: '',
    endTime: ''
  }

  checkAbnormal(row) {
    this.activeSelected = row.id
    this.startTime_chart = row.queStartTime

    this.endTime_chart = row.queEndTime
    this.isChartView = true
    row.startTime = row.queStartTime
    row.endTime = row.queEndTime
    this.specifyTime = {
      startTime: this.startTime_chart,
      endTime: this.endTime_chart
    }
    this.lineParamDeal(row)
  }

  //获取传感器通道参数
  lineParamDeal(row) {
    let reqdata = 'deviceId=' + row.deviceId
    this.jsonApiService.http('realtime/getRealTimeData.html', reqdata).subscribe(result => {
      if (result.success && result.data) {
        row.coefficient = result.data.coef
        this.selectedRow = row
      }
    })
  }

  getSelectChartTime(time) {
    this.startTime_chart = time.startTime
    this.endTime_chart = time.endTime
  }

  runParent(value) {
    this.drawTrackArr = value
  }

  showMapView(val) {
    this.isMapView = val
  }

  // 获取父级公司列表
  inputApi: string = 'company/getParentCompany.html'

  messageEditForm = {
    id: '',
    carNumber: '',
    deviceId: '',
    groupName: '',
    installFactoryId: '',
    installFactoryName: '',
    questioner: '',
    question: '',
    type: '',
    typeDesc: '',
    projectId: '',
    projectName: '',
    queStartTime: '',
    queEndTime: '',
    remark: ''
  }

  // 编辑
  editRow(row) {
    for (let key in this.messageEditForm) {
      this.messageEditForm[key] = row[key]
    }
    $('#messageEditModal').modal('show')
  }

  validatorOptions = {
    excluded: [':disabled'],
    feedbackIcons: {
      valid: 'glyphicon glyphicon-ok',
      invalid: 'glyphicon glyphicon-remove',
      validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
      groupNameInput: {
        trigger: 'blur',
        validators: {
          notEmpty: {
            message: '请输入群名称'
          }
        }
      },
      questionerInput: {
        trigger: 'blur',
        validators: {
          notEmpty: {
            message: '请输入提问题的人'
          }
        }
      },
      questionInput: {
        trigger: 'blur',
        validators: {
          notEmpty: {
            message: '请输入提问题'
          }
        }
      },
      groupMsgType: {
        trigger: 'change',
        validators: {
          notEmpty: {
            message: '请选择归类'
          }
        }
      }
    }
  }

  massageCompanySelected(data) {
    const messageEditForm = this.messageEditForm
    messageEditForm.projectId = data.companyId || ''
    messageEditForm.projectName = data.companyName
  }

  typeEnum = [
    '',
    '设备状态查询',
    '订单-传感器问题',
    '订单-GPS问题',
    '订单-离线',
    '订单-算法问题',
    '订单-磅单问题',
    '订单-数据正常',
    '续费',
    '同步',
    '更改ID',
    '移机-改车牌',
    '临时放行',
    '录车'
  ]

  msgTypeChange() {
    this.messageEditForm.typeDesc = this.typeEnum[this.messageEditForm.type]
  }

  // 关闭编辑弹框
  closeMessageEditModal() {
    $('#messageEditForm').data('bootstrapValidator').resetForm()
    // modal.hide();
    $('#messageEditModal').modal('hide')
  }

  closeChart() {
    this.isChartView = false
  }

  messageEditSubmit() {
    const messageEditForm = { ...this.messageEditForm }
    messageEditForm.groupName = encodeURIComponent(messageEditForm.groupName)
    const that = this
    $('#messageEditForm').bootstrapValidator('validate') //开启验证
    let isValid = $('#messageEditForm').data('bootstrapValidator').isValid() //验证是否通过
    if (isValid) {
      // 项目新增
      that.jsonApiService.http_obj('groupMessage/updateGroupMessage.html', messageEditForm).subscribe(data => {
        that.jsonApiService.message(data.message)
        if (data.success) {
          $('#messageEditForm').data('bootstrapValidator').resetForm()
          // modal.hide();
          $('#messageEditModal').modal('hide')
          that.load()
        }
      })
    }
  }
}
